<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title>基本列表</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>

<body>

    <div id="root">
        <h2>人员列表</h2>
        <input type="text" placeholder="请输入名字" v-model="keyword">
        <button @click="SortType=0">原顺序查询</button>
        <button @click="SortType=1">降序查询</button>
        <button @click="SortType=2">升序排序</button>
        <ul>
            <li v-for="p in filpersons" :key="p.id">{{p.name}}-{{p.sex}}--{{p.age}}</li>
        </ul>
    </div>

    <script type="text/javascript">
        Vue.config.productionTip = false

        new Vue({
            el: '#root',
            data: {
                keyword: '',
                SortType: 0,
                persons: [
                    { id: '001', name: '马冬梅', sex: '女', age: '10' },
                    { id: '003', name: '周杰伦', sex: '男', age: '14' },
                    { id: '002', name: '周冬雨', sex: '女', age: '19' },
                    { id: '004', name: '温兆伦', sex: '男', age: '18' }
                ]
            },

            // filPersons: [],
            // }, watch: {

            //     //开始时先执行一次
            //     keyword: {
            //         immediate: true,
            //         handler(val) {
            //             this.filPersons = this.persons.filter((p) => {
            //                 return p.name.indexOf(val) !== -1
            //             })

            //         }
            //     }

            // },
            //用computed实现
            computed: {
                filpersons() {
                    const arr = this.persons.filter((p) => {
                        return p.name.indexOf(this.keyword) !== -1
                    })
                    if (this.SortType) {
                        arr.sort((p1, p2) => {
                            return this.SortType === 1 ? p2.age - p1.age : p1.age - p2.age
                        })
                    }
                    return arr;
                }
            }
        })
    </script>

</html>